#!/usr/bin/python3
# import serial
# import time
# ser = serial.Serial('/dev/ttyS1', 115200)
# ser.write("@N999X0054Y0100T0\r\n".encode())

# time.sleep(1)
# ser.write("@N003X0215Y0125T1\r\n".encode())
# #ser.write("@N003X0246Y-021T1\r\n".encode())
# # time.sleep(1)
# # while True:
# #     ser.write("@N003X-226Y-118T0\r\n".encode())
# #     time.sleep(1)

# import os
# import time

# def Laser_Init():
#         os.system("gpio mode 10 out")
#         os.system("gpio mode 13 out")
#         os.system("gpio write 10 1")
#         os.system("gpio write 13 1")

# def Laser_Control(laser_id,state):
#     if laser_id == 1 and state == 0:
#         os.system("gpio write 10 1")
#     if laser_id == 1 and state == 1:
#         os.system("gpio write 10 0")
#     if laser_id == 0 and state == 0:
#         os.system("gpio write 13 1")
#     if laser_id == 0 and state == 1:
#         os.system("gpio write 13 0")

# Laser_Init()
# while True:
#     Laser_Control(0, 1)
#     time.sleep(1)
#     Laser_Control(0, 0)
#     time.sleep(1)





# def callback(event):
#     rospy.loginfo(rospy.get_time())

# rospy.init_node('test_node', anonymous=True)
# rospy.Timer(rospy.Duration(1), callback)


# rospy.spin()





# def parse_data(data):
#     try:
#         # 数据格式检查: 包头为'@'，包尾为'\r\n'
#         if data.startswith('@') and data.endswith('\r\n'):
#             content = data[1:-2]
#             number = int(content)
#             print(f"Parsed number: {number}")
            
#             return number
#         else:
#             print("Invalid data format")
#             return None
#     except Exception as e:
#         print(f"Error parsing data: {e}")
#         return None


# import serial
# import time

# # # 串口配置
# import serial
# import time

# ser = serial.Serial('/dev/ttyS6', 9600, timeout=1)

# while True:
#     # print(f"Bytes available: {ser.in_waiting}")  # 打印缓冲区中的字节数
#     if ser.in_waiting > 0:
#         data = ser.readline().decode()
#         print(f"Received data: {data}")
#         res = parse_data(data)
#     time.sleep(0.5)  # 加入延迟，避免过快循环


# # 初始位置
# x = 40
# y = 40

# # 走位的边界
# x_min, x_max = 40, 440
# y_min, y_max = 40, 360

# # 移动方向控制
# move_x = 1  # 1 表示从左到右，-1 表示从右到左
# move_y = 1  # 1 表示从上到下，-1 表示从下到上

# while True:
#     # 发送当前坐标到串口
#     ser.write(f"@X{x:04d}Y{y:04d}T0\r\n".encode())
    
#     # 更新 x, y 坐标，蛇形路径
#     if move_x == 1:  # x 从左到右
#         x += 40
#         if x >= x_max:
#             x = x_max
#             move_x = -1  # 方向切换到从右到左
#             y += 120  # 换行，y增加
#             if y > y_max:
#                 y = y_max
#     else:  # x 从右到左
#         x -= 40
#         if x <= x_min:
#             x = x_min
#             move_x = 1  # 方向切换到从左到右
#             y += 120  # 换行，y增加
#             if y > y_max:
#                 y = y_max

#     # 延时 1 秒
#     time.sleep(1)


import serial

ser_fukong = serial.Serial('/dev/ttyS3', 115200)

def Servo_control(num=0):
        buf = '@' + str(num) + '\r\n'
        ser_fukong.write(buf.encode())
        
Servo_control(8)